package org.aileen.trans.controller;


import jdk.jfr.TransitionTo;
import lombok.extern.slf4j.Slf4j;
import org.aileen.trans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransController {

    @Autowired
    private UserService userService;

    /**
     * 事务回滚
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r1")
    public Boolean registry(String name , String password){
        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );
        int a = 10 / 0;
        return true;
    }


    /**
     * 事务回滚
     * @param name
     * @param password
     * @return
     * @throws IOException
     */

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r2")
    public Boolean r2(String name , String password) throws IOException {
        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );
        if(true){
            throw new IOException();
        }
        return true;
    }



    /**
     * 事务回滚
     * @param name
     * @param password
     * @return
     * @throws IOException
     */

    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT)
    @RequestMapping("/r3")
    public Boolean r3(String name , String password) throws IOException {
        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );
        if(true){
            throw new IOException();
        }
        return true;
    }

    //---------------------------

    /**
     * 事务提交
     */
    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String name , String password){
        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );
        try{
            int a = 10 / 0;
        }catch (Exception e) {
            log.error("发生异常: ", e);//这个异常相当于捕获了异常，事务不会回滚
            // 这里不抛出异常，事务将会提交
        }
        return true;
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r6")
    public Boolean r6(String name , String password){
        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );
        try{
            int a = 10 / 0;
        }catch (Exception e) {
            throw e; // 这里抛出异常，事务将会回滚
        }
        return true;
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r7")
    public Boolean r7(String name , String password){
        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );
        try{
            int a = 10 / 0;
        }catch (Exception e) {
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return true;
    }


}












